package com.mxnet.chongjin.supoman.activity;

import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mxnet.chongjin.supoman.R;
import com.mxnet.chongjin.supoman.config.Api;
import com.mxnet.chongjin.supoman.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

public class MaintainerLoginActivity extends AppCompatActivity implements View.OnClickListener {
    private Context mContext;
    private LinearLayout mLinearLayout;
    private TextView mTextView;
    private Button mButton;
    private EditText mEtAccount, mEtPwd;
    private SharedPreferences mPreferences;
    SharedPreferences.Editor mEditor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintainer_login);
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        mContext = MaintainerLoginActivity.this;
        mPreferences = getSharedPreferences("supoman", MODE_PRIVATE);
        mEditor = mPreferences.edit();

        initView();
        initData();
    }


    public void initView() {
        mLinearLayout = findViewById(R.id.ll_maintainer_login);
        mLinearLayout.setOnClickListener(this);

        mTextView = findViewById(R.id.tv_regist_change_user);
        mTextView.setOnClickListener(this);

        mButton = findViewById(R.id.btn_maintainer_login_login);
        mButton.setOnClickListener(this);

        mEtAccount = findViewById(R.id.et_maintainer_login_account);

        mEtPwd = findViewById(R.id.et_maintainer_login_pwd);
    }

    public void initData() {

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.ll_maintainer_login:
                new Thread() {
                    public void run() {
                        try {
                            Instrumentation inst = new Instrumentation();
                            inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
                break;
            case R.id.tv_regist_change_user:
                intent.setClass(mContext, UserLoginActivity.class);
                mContext.startActivity(intent);
                break;
            case R.id.btn_maintainer_login_login:
                if (!TextUtils.isEmpty(mEtAccount.getText()) && !TextUtils.isEmpty(mEtPwd.getText())) {
                    OkHttpUtils.get()
                            .url(Api.URL_HEADER + Api.EMP_LOGIN)
                            .addParams("userCode", mEtAccount.getText().toString())
                            .addParams("password", mEtPwd.getText().toString())
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Call call, Exception e, int id) {

                                }

                                @Override
                                public void onResponse(String response, int id) {
                                    Intent intent1 = new Intent();
                                    if (!TextUtils.isEmpty(response)) {
                                        try {
                                            JSONObject jsonObject = new JSONObject(response);
                                            boolean isSuccess = jsonObject.optBoolean("success");
                                            if (isSuccess) {
                                                JSONObject data = jsonObject.optJSONObject("data");
                                                mEditor.putInt("useMode", 1);
                                                mEditor.commit();
                                                restartApplication();
                                            } else {
                                                ToastUtils.showToast(mContext, jsonObject.optString("msg"));
                                            }
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                            });
                } else {
                    ToastUtils.showToast(mContext, "请输入账号和密码！");
                }
                break;
        }
    }

    private void restartApplication() {
        final Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }
}
